package com.gooview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;

public class BezierView extends View {
    private Paint paint;

    public BezierView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public BezierView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BezierView(Context context) {
        super(context);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);//设置抗锯齿
        paint.setColor(Color.RED);
    }
    //todo  拖拽圆
    //    private float dragRadius = 18f;//拖拽圆的半径
    //    private PointF dragCenter = new PointF(200f, 240f);//拖拽圆的圆心
    //
    //    // TODO  固定圆
    //    private float stickyRadius = 18f;//固定圆的半径
    //   	private PointF stickyCenter = new PointF(500f, 240f);//固定圆的圆心


    //	private PointF controlPoint = new PointF(125f, 120f);
    //	private double lineK;//todo 斜率
    // private float maxDistance = 80;
    //private boolean isDragOutOfRange = false;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //todo 让整体画布向上偏移一个状态栏高度
        canvas.translate(0, -Utils.getStatusBarHeight(getResources()));
        //todo 圆心坐标计算
        int cx = (getMeasuredWidth() + getPaddingLeft() - getPaddingRight()) / 2;
        int cy = (getMeasuredHeight() + getPaddingTop() + -getPaddingBottom()) / 2;


        float dragRadius = 60f;//拖拽圆的半径
        PointF dragCenter = new PointF(cx - 200, cy);//拖拽圆的圆心

        // TODO  固定圆
        float stickyRadius = 90f;//固定圆的半径
        PointF stickyCenter = new PointF(cx + 200, cy);//固定圆的圆心


        //todo 绘制两个圆
        canvas.drawCircle(dragCenter.x, dragCenter.y, dragRadius, paint);
        canvas.drawCircle(stickyCenter.x, stickyCenter.y, stickyRadius, paint);
        // todo  绘制贝塞尔曲线
        Path path = new Path();
        PointF[] dragP = {new PointF(dragCenter.x, dragCenter.y - dragRadius), new PointF(dragCenter.x, dragCenter.y + dragRadius)};
        PointF[] stickyP = {new PointF(stickyCenter.x, stickyCenter.y - stickyRadius), new PointF(stickyCenter.x, stickyCenter.y + stickyRadius)};
        PointF controlP = new PointF(dragCenter.x + (stickyCenter.x - dragCenter.x) / 2, dragCenter.y);


        path.moveTo(stickyP[0].x, stickyP[0].y);
        path.quadTo(controlP.x, controlP.y, dragP[0].x, dragP[0].y);
        path.lineTo(dragP[1].x, dragP[1].y); //fixme  注意不是moveTo,要连线
        path.quadTo(controlP.x, controlP.y, stickyP[1].x, stickyP[1].y);
        canvas.drawPath(path, paint);
    }


}
